package Linki;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Hashtable;
import java.util.LinkedList;

import javax.imageio.ImageIO;

import A002_Obrazek._A_DaneObrazka;
import A002_Obrazek._A_OBRAZEK;

/**
 * @author TomekUser2
 *
 */
public class _A_LADUJ_SERtoBUFEIMG extends _A_Path {

	//==============================================================================
	//==============================================================================
	//===========ZmmienneObszrow====================================================
	//==============================================================================
	//==============================================================================
	/**
	 * 
	 * @parm Obszry <b>klucze</b> to nazwy obszrow 
	 * Tablice z Obrazkami elementow szukanych
	 * 
	 */

	public static  Hashtable<String, _A_OBRAZEK> _ListaObrazków;
		
	
	//==============================================================================
	 
	/**
	 * @param _Szukane - kolekcja obszarow, czyli klusz to nazwa obszaru, a wartosc to Obrazki do jego znalezienia
	 * Hashtable <String, Hashtable <String, LinkedList<int[][]>>>()
	 * np. Hashtable <PALLADIUM, Hashtable <pal_1, LinkedList<int[][]>>>()
	 * PALLADIUM zbior elementow szukanych paladium
	 * w nim sie kryje Hashtable <String.... tu sie kryją LinkedList<int[][] - w ktorych sa zapisane tablice z kolorami
	 */
	public _A_LADUJ_SERtoBUFEIMG(){
		super();
		_ListaObrazków = new Hashtable<String, _A_OBRAZEK>();
		
		initSzukane (iGlobalPath + iKatSys + "SZUKANE");
	}
	
	public void initSzukane ( String sciezka) {
		System.out.println ("Laduje foldery");

		File[] folders; //nazwy folderow
		
			folders = new File(sciezka).listFiles(
				new FileFilter(){
					@Override
					public boolean accept(File file){
						return !file.isHidden() && file.isDirectory();
						}
					});
			
			if(folders !=null && folders.length != 0)
			System.out.println ("Ilosc folderow w "+ sciezka + " - " + folders.length);
			else return;
		
		// sparwdza ilozs plikow w karzdym folderze
		for (File folder: folders ){
			
			File[] files; //lista plikow
			
			files = new File (folder +"/").listFiles(
					new FileFilter(){
						@Override
						public boolean accept(File file){
							return !file.isHidden() && file.isFile();
							}
						});
			try{	
			System.out.println ("lista plikow w folderze /" + folder + "/ " + files.length);
			} catch (NullPointerException n) {
				System.out.println("BRAK PLIOW W   /" + folder.getName()+"/");	
			}
			
			String ielemnt;
			String ikey="";
			int i;
			
			LinkedList<int[][]> ifilm = null; // członkowie szczepu
			
				if (files != null && files.length > 0){
															
					for (File file : files){
						
						ielemnt = file.getName();
						i = ielemnt.indexOf(".");
						//SER_________________________________________
						if (i!= -1 && ielemnt.substring(i).equals(".ser")){
							ikey = ielemnt.substring(0, i);
						/*	System.out.println();
							System.out.println("FOLDER "+ folder.getName() + "  PLIK " + ikey);
							System.out.println("FOLDER "+ folder.getName() + "  Rozszerzenie " + ielemnt.substring(i));
							System.out.println("FOLDER "+ folder.getName() + "  Sciezka " + file);
							System.out.println();*/
							
							// wczytuje objekt linkedList<int[][]>
							try{
						    	 
						      FileInputStream fis = new FileInputStream(file);
						      ObjectInputStream ois = new ObjectInputStream(fis);
						      
						      ifilm  = (LinkedList<int[][]>) ois.readObject();
						
						      ois.close();
						
						
						  } catch (FileNotFoundException e) {e.printStackTrace();
						  } catch (IOException e) {e.printStackTrace();
						     } catch (ClassNotFoundException e) {e.printStackTrace();
						     }
							
							//iWieleFilmow.put(ikey, ifilm);
														
							int klatki = ifilm.size();
							// przetwarzanie na Buffered Images
							BufferedImage tmpBuf[] = new BufferedImage [klatki];
							int kl = ifilm.size()-1;
							
							for (int[][] to : ifilm){
								tmpBuf[kl] = new BufferedImage(to.length, to[0].length, BufferedImage.TYPE_3BYTE_BGR);
								
								for (int y = 0; y < to[0].length; y++){
									for (int x = 0; x < to.length; x++){
										tmpBuf[kl].setRGB(x, y, to[x][y]);
									}
								}
								
								kl--;

							}	
							
							try {
								_ListaObrazków.put(ikey, new _A_OBRAZEK(ikey, tmpBuf));
							} catch (Exception e) {
								e.printStackTrace();
							}

						}
						//END SER___________________________________________________________
						
						//BMP_________________________________________
						if (i!= -1 && ielemnt.substring(i).equals(".bmp")){
							BufferedImage _tmpBuf = null;
							ikey = ielemnt.substring(0, i);
							try {
								_tmpBuf = ImageIO.read(new File(""+file));
							} catch (IOException e) {
								System.err.println("BRAK OBRAZKA buffered image informacja z _A_DaneObrazka");
							}
							
							try {
								_ListaObrazków.put(ikey, new _A_OBRAZEK(ikey, _tmpBuf));
								//System.out.println("LAUJE DO LISTY OBRAZKOW " + ikey + " ilosc " + _tmpBuf.length);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}	
					
			}
		}

		
	}
	
	//==================================================================================
	//==================Szukanie elmentow===============================================
	//==================================================================================

	
	public static void main(String[] args) {
		_A_Path path = new _A_Path();
		_A_LADUJ_SERtoBUFEIMG _initL = new _A_LADUJ_SERtoBUFEIMG(); 
		
	}
}
